package com.xb.loan.usercenter.user.user.service.impl;

import com.xb.loan.log.exception.BizException;
import com.xb.loan.usercenter.service.user.IUserActivityService;
import com.xb.loan.usercenter.service.user.bean.UserActivityVO;
import com.xb.loan.usercenter.user.user.dao.UserActivityDao;
import com.xb.loan.usercenter.user.user.dao.bean.UserActivity;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.common.ObjectOperation;
import com.xb.loan.util.log.DefaultLoggerFacotry;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userActivityService")
public class UserActivityServiceImpl implements IUserActivityService {

	private Logger logger = DefaultLoggerFacotry.getLogger(UserActivityServiceImpl.class);
	
	@Autowired
	private UserActivityDao userActivityDao;
	
	@Override
	public void add(UserActivityVO userActivityVO) throws BizException {
		
		UserActivity userActivity = ObjectOperation.convertByClasses(userActivityVO, UserActivity.class);
        String id =  FormatOperation.assemblySeq("common", 4, FormatOperation.formatDateToYMDHms(new Date()), "");
        userActivity.setId(id);
		userActivityDao.insertSelective(userActivity);
	}

	@Override
	public void delete(String id) throws BizException {
		
		userActivityDao.delete(id);
	}

	@Override
	public void update(UserActivityVO userActivityVO) throws BizException {
		
		UserActivity userActivity = ObjectOperation.convertByClasses(userActivityVO, UserActivity.class);
		userActivityDao.update(userActivity);
	}

	@Override
	public UserActivityVO query(String id) throws BizException {
		
		UserActivity userActivity = userActivityDao.query(id);
		return ObjectOperation.convertByClasses(userActivity, UserActivityVO.class);
		
	}

	public List<UserActivityVO> queryUserActivity(int pageNum, int numPerPage) throws BizException{
		
		Map<String,Integer> map = new HashMap<String,Integer>();
		map.put("pageNum", pageNum);
		map.put("numPerPage", numPerPage);
		List<UserActivity> list = userActivityDao.queryAppUserActivity(map);
		return ObjectOperation.convertByClassesList(list, UserActivityVO.class);
	}
	
	public int queryUserActivityCount() throws BizException{
		
		return userActivityDao.queryAppUserActivityCount();
	}
	
	@Override
	public List<UserActivityVO> queryUserActivity(String title, String content,
                                                  Integer type, String startTime, String endTime, int pageNum,
                                                  int numPerPage) throws BizException {
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("title", title);
		map.put("content", content);
		map.put("type", type);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("pageNum", pageNum);
		map.put("numPerPage", numPerPage);
		List<UserActivity> list = userActivityDao.queryUserActivity(map);
		return ObjectOperation.convertByClassesList(list, UserActivityVO.class);
	}

	@Override
	public int queryUserActivityCount(String title, String content,
                                      Integer type, String startTime, String endTime) throws BizException {

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("title", title);
		map.put("content", content);
		map.put("type", type);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return userActivityDao.queryUserActivityCount(map);
	}


}
